/*
 * @message: 描述
 * @Author: Roy
 * @Email: @163.com
 * @Github: @163.com
 * @Date: 2021-11-20 14:25:57
 * @LastEditors: Roy
 * @LastEditTime: 2021-11-21 21:24:35
 * @Deprecated: 否
 * @FilePath: /train-ticket/src/pages/query/store/reducer.js
 */
import * as actionTypes from './actionTypes'
import { ORDER_DEPART } from '../../../const/constant'
const defaultState = {
    from: null,
    to: null,
    departDate: Date.now(),
    highSpeed: false,//是否选择了高铁动车
    trainList: [],//车票列表
    orderType: ORDER_DEPART,//排序默认出发早->晚
    onlyTickets: false,//是否只看有票
    ticketTypes: [],//所有的被选项
    checkedTicketTypes: {},//选中类型
    trainTypes: [],//车次类型
    checkedTrainTypes: {},//选中的车次类型
    departStations: [],//出发车站
    checkedDepartStations: {},//选中出发车站
    arriveStations: [],//到达车站
    checkedArriveStations: {},//选中的到达车站
    departTimeStart: 0,//出发时间起始点
    departTimeEnd: 24,//出发时间的截止点
    arriveTimeStart: 0,//到达时间的起点
    arriveTimeEnd: 24,//到达时间的截止点
    isFiltersVisible: false,//是否显示隐藏
    searchParsed: false,//是否解析完成
};

// eslint-disable-next-line import/no-anonymous-default-export
export default (state = defaultState, action) => {
    let newState = JSON.parse(JSON.stringify(state));
    switch (action.type) {
        case actionTypes.ACTION_SET_FROM:
            newState.from = action.payload;
            return newState;
        case actionTypes.ACTION_SET_TO:
            newState.to = action.payload;
            return newState;
        case actionTypes.ACTION_SET_DEPART_DATE:
            newState.departDate = action.payload;
            return newState;
        case actionTypes.ACTION_SET_HIGH_SPEED:
            newState.highSpeed = action.payload;
            let newCheckedTrainTypes = { ...newState.checkedTrainTypes };
            if (newState.highSpeed) {
                newCheckedTrainTypes[1] = true;
                newCheckedTrainTypes[5] = true;
            } else {
                delete newCheckedTrainTypes[1];
                delete newCheckedTrainTypes[5];
            }
            newState.checkedTrainTypes = newCheckedTrainTypes;
            return newState;
        case actionTypes.ACTION_SET_CHECKED_TRAIN_TYPES:
            let checkedTrainTypes = action.payload;
            newState.highSpeed = Boolean(checkedTrainTypes[1] && checkedTrainTypes[5]);
            state.checkedTrainTypes = checkedTrainTypes;
            return newState;
        case actionTypes.ACTION_SET_TRAIN_LIST:
            newState.trainList = action.payload;
            return newState;
        case actionTypes.ACTION_SET_ORDER_TYPE:
            newState.orderType = action.payload;
            return newState;
        case actionTypes.ACTION_SET_ONLY_TICKETS:
            newState.onlyTickets = action.payload;
            return newState;
        case actionTypes.ACTION_SET_TICKET_TYPES:
            newState.ticketTypes = action.payload;
            return newState;
        case actionTypes.ACTION_SET_CHECKED_TICKET_TYPES:
            newState.checkedTicketTypes = action.payload;
            return newState;
        case actionTypes.ACTION_SET_TRAIN_TYPES:
            newState.trainTypes = action.payload;
            return newState;
        case actionTypes.ACTION_SET_DEPART_STATIONS:
            newState.departStations = action.payload;
            return newState;
        case actionTypes.ACTION_SET_CHECKED_DEPART_STATIONS:
            newState.checkedDepartStations = action.payload;
            return newState;
        case actionTypes.ACTION_SET_ARRIVE_STATIONS:
            newState.arriveStations = action.payload;
            return newState;
        case actionTypes.ACTION_SET_CHECKED_ARRIVE_STATIONS:
            newState.checkedArriveStations = action.payload;
            return newState;
        case actionTypes.ACTION_SET_DEPART_TIME_START:
            newState.departTimeStart = action.payload;
            return newState;
        case actionTypes.ACTION_SET_DEPART_TIME_END:
            newState.departTimeEnd = action.payload;
            return newState;
        case actionTypes.ACTION_SET_ARRIVE_TIME_START:
            newState.arriveTimeStart = action.payload;
            return newState;
        case actionTypes.ACTION_SET_ARRIVE_TIME_END:
            newState.arriveTimeEnd = action.payload;
            return newState;
        case actionTypes.ACTION_SET_IS_FILTERS_VISIBLE:
            newState.isFiltersVisible = action.payload;
            return newState;
        case actionTypes.ACTION_SET_SEARCH_PARSED:
            newState.searchParsed = action.payload;
            return newState;
        default:
            break;
    }
    return state;

};
